Index: lib/form/filemanager.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/form/filemanager.php,v
retrieving revision 1.2
diff -u -p -r1.2 filemanager.php
--- lib/form/filemanager.php	19 Nov 2008 21:53:37 -0000	1.2
+++ lib/form/filemanager.php	1 Dec 2008 04:16:44 -0000
@@ -1,6 +1,7 @@
 <?php  // $Id: filemanager.php,v 1.2 2008/11/19 21:53:37 skodak Exp $
 
 require_once('HTML/QuickForm/element.php');
+require_once(dirname(dirname(dirname(__FILE__))) . '/lib/filelib.php');
 
 class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
     protected $_helpbutton = '';
@@ -89,8 +90,44 @@ class MoodleQuickForm_filemanager extend
         }
     }
 
+    function _get_draftfiles($draftid, $suffix) {
+        global $USER, $CFG;
+        $html = '';
+        if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) {
+        }
+        $contextid = $context->id;
+        $filearea  = 'user_draft';
+
+        $browser = get_file_browser();
+        $fs      = get_file_storage();
+        $filepath = '/';
+        if (!$directory = $fs->get_file($context->id, 'user_draft', $draftid, $filepath, '.')) {
+            $directory = new virtual_root_file($context->id, 'user_draft', $draftid);
+            $filepath = $directory->get_filepath();
+        }
+        $files = $fs->get_directory_files($context->id, 'user_draft', $draftid, $directory->get_filepath());
+        $parent = $directory->get_parent_directory();
+        $html .= '<ul id="draftfiles-'.$suffix.'">';
+        foreach ($files as $file) {
+            $filename    = $file->get_filename();
+            $filenameurl = rawurlencode($filename);
+            $filepath    = $file->get_filepath();
+            $filesize    = $file->get_filesize();
+            $filesize    = $filesize ? display_size($filesize) : '';
+            $mimetype = $file->get_mimetype();
+            $icon    = mimeinfo_from_type('icon', $mimetype);
+            $viewurl = $browser->encodepath("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$draftid".$filepath.$filename, false, false);
+            $html .= '<li>';
+            $html .= "<a href=\"$viewurl\"><img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" />&nbsp;".s($filename)." ($filesize)</a> ";
+            $html .= "<a href=\"###\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" /></a>";;
+            $html .= '</li>';
+        }
+        $html .= '</ul>';
+        return $html;
+    }
+
     function toHtml() {
-        global $CFG, $USER;
+        global $CFG, $USER, $COURSE;
 
         // security - never ever allow guest/not logged in user to upload anything or use this element!
         if (isguestuser() or !isloggedin()) {
@@ -114,12 +151,42 @@ class MoodleQuickForm_filemanager extend
             $draftitemid = $this->getValue();
         }
 
-        $editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$draftitemid&amp;subdirs=$subdirs&amp;maxbytes=$maxbytes";
+        if ($COURSE->id == SITEID) {
+            $context = get_context_instance(CONTEXT_SYSTEM);
+        } else {
+            $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
+        }
+
+        $repo_info = repository_get_client($context);
+        $suffix = $repo_info['suffix'];
 
-        $str = $this->_getTabs();
-        $str .= '<input type="hidden" name="'.$elname.'" value="'.$draftitemid.'" />';
-        $str .= '<object type="text/html" id="'.$id.'" data="'.$editorurl.'" height="160" width="600" style="border:1px solid #000">Error</object>'; // TODO: localise, fix styles, etc.
+        $html = $this->_get_draftfiles($draftitemid, $suffix);
 
+        $str = $this->_getTabs();
+        $str .= $html;
+        $str .= $repo_info['css'];
+        $str .= $repo_info['js'];
+        $str .= <<<EOD
+<script type="text/javascript">
+function updatefile_$suffix(obj) {
+    var list = document.getElementById('draftfiles-$suffix');
+    var html = '<li><a href="'+obj['url']+'">'+obj['file']+'</a></li>';
+    list.innerHTML += html;
+}
+function callpicker_$suffix() {
+    document.body.className += ' yui-skin-sam';
+    var picker = document.createElement('DIV');
+    picker.id = 'file-picker-$suffix';
+    picker.className = 'file-picker';
+    document.body.appendChild(picker);
+    var el=document.getElementById('$id');
+    openpicker_$suffix({'env':'filemanager', 'target':el, 'itemid': $draftitemid, 'callback':updatefile_$suffix})
+}
+</script>
+<div>
+    <input value="Add a file" onclick="callpicker_$suffix()" type="button" />
+</div>
+EOD;
         return $str;
     }
 
Index: lib/form/filepicker.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/form/filepicker.php,v
retrieving revision 1.21
diff -u -p -r1.21 filepicker.php
--- lib/form/filepicker.php	26 Nov 2008 03:26:34 -0000	1.21
+++ lib/form/filepicker.php	1 Dec 2008 04:16:44 -0000
@@ -83,8 +83,8 @@ class MoodleQuickForm_filepicker extends
 
         $str .= <<<EOD
 <script type="text/javascript">
-function updatefile_$suffix(str) {
-    document.getElementById('repo_info_$suffix').innerHTML = str;
+function updatefile_$suffix(obj) {
+    document.getElementById('repo_info_$suffix').innerHTML = obj['file'];
 }
 function callpicker_$suffix() {
     document.body.className += ' yui-skin-sam';
@@ -93,7 +93,7 @@ function callpicker_$suffix() {
     picker.className = 'file-picker';
     document.body.appendChild(picker);
     var el=document.getElementById('$id');
-    openpicker_$suffix({'env':'form', 'target':el, 'callback':updatefile_$suffix})
+    openpicker_$suffix({'env':'filepicker', 'target':el, 'callback':updatefile_$suffix})
 }
 </script>
 EOD;
Index: repository/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/repository/lib.php,v
retrieving revision 1.143
diff -u -p -r1.143 lib.php
--- repository/lib.php	26 Nov 2008 07:03:10 -0000	1.143
+++ repository/lib.php	1 Dec 2008 04:16:48 -0000
@@ -698,17 +698,19 @@ abstract class repository {
         $entry->filearea  = $filearea;
         $entry->contextid = $context->id;
         $entry->filename  = $name;
-        $entry->filepath  = '/'.uniqid().'/';
+        //$entry->filepath  = '/'.uniqid().'/';
+        $entry->filepath  = '/';
         $entry->timecreated  = $now;
         $entry->timemodified = $now;
+        $entry->userid       = $USER->id;
         if(is_numeric($itemid)) {
             $entry->itemid = $itemid;
         } else {
             $entry->itemid = 0;
         }
         $entry->mimetype     = mimeinfo('type', $path);
-        $entry->userid       = $USER->id;
         $fs = get_file_storage();
+        echo_fb($entry);
         $browser = get_file_browser();
         if ($file = $fs->create_file_from_pathname($entry, $path)) {
             $delete = unlink($path);
@@ -750,7 +752,7 @@ abstract class repository {
      * @param bool $override override file if exists
      * @return mixed stored_file object or false if error; may throw exception if duplicate found
      */
-    public static function store_to_filepool($elname, $filearea='user_draft', $filepath='/', $filename = '', $override = false) {
+    public static function store_to_filepool($elname, $filearea='user_draft', $filepath='/', $itemid='', $filename = '', $override = false) {
         global $USER;
         if (!isset($_FILES[$elname])) {
             return false;
@@ -760,7 +762,9 @@ abstract class repository {
             $filename = $_FILES[$elname]['name'];
         }
         $context = get_context_instance(CONTEXT_USER, $USER->id);
-        $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
+        if (empty($itemid)) {
+            $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
+        }
         $fs = get_file_storage();
         $browser = get_file_browser();
 
Index: repository/ws.php
===================================================================
RCS file: /cvsroot/moodle/moodle/repository/ws.php,v
retrieving revision 1.46
diff -u -p -r1.46 ws.php
--- repository/ws.php	26 Nov 2008 07:03:10 -0000	1.46
+++ repository/ws.php	1 Dec 2008 04:16:48 -0000
@@ -12,6 +12,7 @@
     $env   = optional_param('env', 'form', PARAM_ALPHA);      // opened in editor or moodleform
     $file  = optional_param('file', '', PARAM_RAW);           // file to download
     $title = optional_param('title', '', PARAM_FILE);         // new file name
+    $itemid = optional_param('itemid', '', PARAM_INT);
     $action = optional_param('action', '', PARAM_ALPHA);
     $ctx_id  = optional_param('ctx_id', SITEID, PARAM_INT);   // context ID
     $repo_id = optional_param('repo_id', 1, PARAM_INT);       // repository ID
@@ -153,13 +154,15 @@ EOD;
             break;
         case 'download':
             $path = $repo->get_file($file, $title);
-            $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
+            if (empty($itemid)) {
+                $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
+            }
             try {
                 if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $path)) {
                     echo json_encode(array('url'=>$path, 'id'=>$path, 'file'=>$path));
                 } else {
                     $info = repository::move_to_filepool($path, $title, $itemid);
-                    if ($env == 'form'){
+                    if ($env == 'filepicker' || $env == 'filemanager'){
                         echo json_encode($info);
                     } else if ($env == 'editor') {
                         echo json_encode($info);
Index: repository/javascript.php
===================================================================
RCS file: /cvsroot/moodle/moodle/repository/javascript.php,v
retrieving revision 1.36
diff -u -p -r1.36 javascript.php
--- repository/javascript.php	27 Nov 2008 03:32:48 -0000	1.36
+++ repository/javascript.php	1 Dec 2008 04:16:49 -0000
@@ -806,6 +806,9 @@ _client.download = function() {
     var file = document.getElementById('fileurl-$suffix').value;
     _client.loading('download', title);
     var params = [];
+    if(_client.itemid){
+        params['itemid']=_client.itemid;
+    }
     params['env']=_client.env;
     params['file']=file;
     params['title']=title;
@@ -838,14 +841,14 @@ _client.login = function() {
     var trans = YAHOO.util.Connect.asyncRequest('POST',
             '$CFG->httpswwwroot/repository/ws.php?action=sign', _client.req_cb, _client.postdata(params));
 }
-_client.end = function(str) {
-    if(_client.env=='form') {
-        _client.target.value = str['id'];
-    }else{
-        _client.target.value = str['url'];
+_client.end = function(obj) {
+    if(_client.env=='filepicker') {
+        _client.target.value = obj['id'];
+    }else if(_client.env=='editor'){
+        _client.target.value = obj['url'];
         _client.target.onchange();
     }
-    _client.formcallback(str['file']);
+    _client.formcallback(obj);
     _client.instance.hide();
     _client.viewfiles();
 }
@@ -992,6 +995,9 @@ function openpicker_$suffix(params) {
     if(!repository_client_$suffix.instance) {
         repository_client_$suffix.env = params.env;
         repository_client_$suffix.target = params.target;
+        if(params.itemid){
+            repository_client_$suffix.itemid = params.itemid;
+        }
         if(params.mimetype) {
             repository_client_$suffix.mimetype = params.mimetype;
         } else {
Index: repository/upload/repository.class.php
===================================================================
RCS file: /cvsroot/moodle/moodle/repository/upload/repository.class.php,v
retrieving revision 1.9
diff -u -p -r1.9 repository.class.php
--- repository/upload/repository.class.php	26 Nov 2008 07:03:10 -0000	1.9
+++ repository/upload/repository.class.php	1 Dec 2008 04:16:49 -0000
@@ -24,9 +24,10 @@ class repository_upload extends reposito
     public function __construct($repositoryid, $context = SITEID, $options = array()){
         global $SESSION, $action, $CFG;
         parent::__construct($repositoryid, $context, $options);
+        $itemid = optional_param('itemid', '', PARAM_INT);
         if($action=='upload'){
             $filepath = '/'.uniqid().'/';
-            $this->info = repository::store_to_filepool('repo_upload_file', 'user_draft', $filepath);
+            $this->info = repository::store_to_filepool('repo_upload_file', 'user_draft', $filepath, $itemid);
         }
     }
 
